﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using RpgLibrary.ItemClasses;
using RpgLibrary.TrapClasses;
using RpgLibrary.SkillClasses;
using RpgLibrary;

namespace RpgEditor
{
    public partial class FormChestDetails : FormDetailBase, IDetailForm
    {
        #region Field Region
        
        ChestData chest;
        
        #endregion

        #region Property Region
        
        public ChestData Chest
        {
            get { return chest; }
            set { chest = value; }
        }

        public override IRpgDataObject DataObject
        {
            get
            {
                return (IRpgDataObject)chest;
            }
            set
            {
                chest = (ChestData)value;
            }
        }

        #endregion

        public FormChestDetails()
        {
            InitializeComponent();
            
            InitializeDifficultyCombo();
            
            cbLock.CheckedChanged += new EventHandler(cbLock_CheckedChanged);
            cbTrap.CheckedChanged += new EventHandler(cbTrap_CheckedChanged);
            
            SetDataObjectValueFunction = SetChestValue;
            LoadDataObjectValueFunction = LoadChestValue;
        }

        private void InitializeDifficultyCombo()
        {
            foreach (string s in Enum.GetNames(typeof(DifficultyLevel)))
            {
                cboDifficulty.Items.Add(s);
            }

            cboDifficulty.SelectedIndex = 0;
        }

        #region Form Event Handler Region

        void LoadChestValue()
        {
            if (chest != null)
            {
                tbName.Text = chest.Name;
                
                cbLock.Checked = chest.IsLocked;
                tbKeyName.Text = chest.KeyName;
                tbKeyType.Text = chest.KeyType;
                nudKeysNeeded.Value = (decimal)chest.KeysRequired;
                
                tbKeyName.Enabled = chest.IsLocked;
                tbKeyType.Enabled = chest.IsLocked;
                nudKeysNeeded.Enabled = chest.IsLocked;
                
                cbTrap.Checked = chest.IsTrapped;
                tbTrapName.Text = chest.TrapName;
                tbTrapName.Enabled = chest.IsTrapped;
                
                nudMinGold.Value = (decimal)chest.MinGold;
                nudMaxGold.Value = (decimal)chest.MaxGold;
            }
        }

        #endregion

        #region Check Box Event Handler Region

        void cbLock_CheckedChanged(object sender, EventArgs e)
        {
            cboDifficulty.Enabled = cbLock.Checked;
            tbKeyName.Enabled = cbLock.Checked;
            tbKeyType.Enabled = cbLock.Checked;
            nudKeysNeeded.Enabled = cbLock.Checked;
        }

        void cbTrap_CheckedChanged(object sender, EventArgs e)
        {
            tbTrapName.Enabled = cbTrap.Checked;
        }

        #endregion

        #region Button Event Handler Region

        void btnAdd_Click(object sender, EventArgs e)
        {
        }

        void btnRemove_Click(object sender, EventArgs e)
        {
        }

        bool SetChestValue()
        {
            if (string.IsNullOrEmpty(tbName.Text))
            {
                MessageBox.Show("You must enter a name for the chest.");
                return false;
            }

            if (cbTrap.Checked && string.IsNullOrEmpty(tbTrapName.Text))
            {
                MessageBox.Show("You must supply a name for the trap on the chest.");
                return false;
            }

            ChestData data = new ChestData();
            data.Name = tbName.Text;
            data.IsLocked = cbLock.Checked;
            if (cbLock.Checked)
            {
                data.DifficultyLevel = (DifficultyLevel)cboDifficulty.SelectedIndex;
                data.KeyName = tbKeyName.Text;
                data.KeyType = tbKeyType.Text;
                data.KeysRequired = (int)nudKeysNeeded.Value;
            }

            data.IsTrapped = cbTrap.Checked;
            if (cbTrap.Checked)
            {
                data.TrapName = tbTrapName.Text;
            }

            data.MinGold = (int)nudMinGold.Value;
            data.MaxGold = (int)nudMaxGold.Value;
            
            chest = data;

            return true;
        }

        #endregion

        private void nudMinGold_ValueChanged(object sender, EventArgs e)
        {
            if (nudMinGold.Value > nudMaxGold.Value)
                nudMinGold.Value = nudMaxGold.Value;
        }

        private void nudMaxGold_ValueChanged(object sender, EventArgs e)
        {
            if (nudMinGold.Value > nudMaxGold.Value)
                nudMaxGold.Value = nudMinGold.Value;
        }

    }
}
